package com.vlong.town.controller.base;

import com.vlong.town.util.common.TownConstant;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * BaseErrorController
 * @author Assassin
 */
@Controller
public class BaseErrorController implements org.springframework.boot.web.servlet.error.ErrorController {

    @RequestMapping(value = "/error")
    public String handleError(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        String userType = (String) request.getSession().getAttribute(TownConstant.TOWN_SESSION_USER_TYPE);
        String page = "redirect:/client/index/main";
        if(userType != null) {
            if(userType.equals(TownConstant.TOWN_SESSION_USER_TYPE_ADMIN)) {
                page = "redirect:/main";
            } else {
                page = "redirect:/client/index/main";
            }
        }
        if(statusCode == 401){
            return page;
        } else if(statusCode == 404){
            return page;
        } else if(statusCode == 403){
            return page;
        } else{
            return page;
        }
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }

} 